/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2017 All Rights Reserved.
 */
package xiaohong.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @author yinshipeng
 * @version Id: ThreadPoolUtils.java, v 0.1 2017/8/29 18:00 yinShiPeng Exp $$
 */
public class ThreadPoolUtils {
    /**
     * 定义核心线程数，并行线程数
     */
    private static final int                           CORE_POOL_SIZE      = 8;
    /**
     * 线程池最大线程数：除了正在运行的线程额外保存多少个线程
     */
    private static final int                           MAX_POOL_SIZE       = 32;
    /**
     * 额外线程空闲状态生存时间
     */
    private static final int                           KEEP_ALIVE_TIME     = 5000;
    /**
     * 队列
     */
    private static final LinkedBlockingQueue<Runnable> linkedBlockingQueue = new LinkedBlockingQueue<Runnable>();
    /**
     * 线程池
     */
    private static final ExecutorService               executor;

    private static final ThreadFactory namedThreadFactory = Executors.defaultThreadFactory();

    // 静态代码块，在类被加载的时候进入
    static {
        executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
            TimeUnit.SECONDS, linkedBlockingQueue, namedThreadFactory);
    }

    private ThreadPoolUtils() {
    }

    public static void execute(Runnable runnable) {
        executor.execute(runnable);
    }

}